package com.hhdys.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;


public class CacheUtil {
	
	
	private static final CacheManager CACHE_MANAGER=CacheManager.create(CacheUtil.class.getResource("../../../ehcache.xml"));
	
	public static void save(String cacheName,String key,Object val){
		Cache cache = CACHE_MANAGER.getCache(cacheName);
		if(cache==null){
			cache=new Cache(cacheName, 10000, true, false, 0, 0);
			CACHE_MANAGER.addCache(cache);
		}
		cache.put(new Element(key, val));
	}
	
	public static Object get(String cacheName,String key){
		Cache cache = CACHE_MANAGER.getCache(cacheName);
		if(cache==null){
			return null;
		}
		Element element = cache.get(key);
		if(element==null){
			return null;
		}
		return element.getObjectValue();
	}
	
	public static void clear(String cacheName,String key){
		Cache cache = CACHE_MANAGER.getCache(cacheName);
		if(cache!=null){
			cache.remove(key);
		}
	}
}
